<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类似文案" prop="tabloid">
        <el-input v-model="queryParams.tabloid" placeholder="请输入类似文案" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="作者名字" prop="author">
        <el-input v-model="queryParams.author" placeholder="请输入作者名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="评分" prop="rata">
        <el-input v-model="queryParams.rata" placeholder="请输入评分" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
          @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="浏览量" align="center" prop="views" />
      <el-table-column label="分类，默认音乐资讯" align="center" prop="category" />
      <el-table-column label="类似文案" align="center" prop="tabloid" />
      <el-table-column label="作者名字" align="center" prop="author" />
      <el-table-column label="创建时间" align="center" prop="gmtCreate" width="180">
      </el-table-column>
      <el-table-column label="文章内容" align="center" prop="content" />
      <el-table-column label="评分" align="center" prop="rata" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template #default="scope">
          <el-switch v-model="scope.row.status" size="small" :active-value="0" inactive-value="1" active-text="通过" inactive-text="未通过" @change="updateStatus(scope.row)" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination :total="total" :page-size="queryParams.pageSize" v-model:current-page="queryParams.pageNum"
      @current-change="getList" />

    <!-- 添加或修改ArticleAdmin对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="浏览量" prop="views">
          <el-input v-model="form.views" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="分类，默认音乐资讯" prop="category">
          <el-input v-model="form.category" placeholder="请输入分类，默认音乐资讯" />
        </el-form-item>
        <el-form-item label="类似文案" prop="tabloid">
          <el-input v-model="form.tabloid" placeholder="请输入类似文案" />
        </el-form-item>
        <el-form-item label="作者名字" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者名字" />
        </el-form-item>
        <el-form-item label="文章内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="评分" prop="rata">
          <el-input v-model="form.rata" placeholder="请输入评分" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script setup>
import { listArticle, getArticle, delArticle, addArticle, updateArticle, updateArticleStatus } from "@/api/music/music.ts";
import { ref, getCurrentInstance, computed, onBeforeMount, onMounted, toRefs, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import { UToast } from 'undraw-ui'


const updateStatus=(row)=>{
  updateArticleStatus(row.id,row.status)
}


// 遮罩层
const loading = ref(false)
// 选中数组
const ids = ref([])
// 非单个禁用
var single = ref(true)
// 非多个禁用
var multiple = ref(true)
// 显示搜索条件
var showSearch = true
// 总条数
const total = ref(0)
// 用户管理表格数据
const usersList = ref([])
// 弹出层标题
const title = ref("")
// 是否显示弹出层
const open = ref(false)

// 查询参数
const queryParams = ref({
  id: '',
  pageNum: 1,
  pageSize: 10,
  title: null,
  views: null,
  category: null,
  tabloid: null,
  author: null,
  gmtCreate: null,
  content: null,
  rata: null,
  status: null
})
// 表单参数
const form = ref({

})
// 表单校验
const rules = ref({
})


onMounted(() => {
  getList()
}
)

/** 查询ArticleAdmin列表 */
const getList = async () => {
  const response = await listArticle(queryParams.value)
  usersList.value = response.rows;
  total.value = response.total;

}
// 取消按钮
const cancel = () => {
  open.value = false;
  reset();
}
// 表单重置
const reset = () => {
  form.value = {};
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
}


// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加用户管理";
}

/** 修改按钮操作 */
const handleUpdate = async (row) => {
  form.value = {}
  const userId = row.id || ids.value
  const response = await getArticle(userId)
  form.value = response.data;
  open.value = true;
  title.value = "修改文章管理";

}

/** 提交按钮 */
const submitForm = () => {
  if (form.value.id != null) {
    updateArticle(form.value).then(response => {
      UToast({ message: '修改成功!', type: 'success' })
      open.value = false;
      getList();
    });
  } else {
    addArticle(form.value).then(response => {
      UToast({ message: '新增成功!', type: 'success' })
      open.value = false;
      getList();
    });
  }
}

/** 删除按钮操作 */
const handleDelete = (row) => {
  const userIds = row.id || ids.value;
  ElMessageBox.confirm(
    '是否确认删除用户id为"' + userIds + '"的数据项？',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      delArticle(userIds).then(response => {
        UToast({ message: '删除成功!', type: 'success' })
        getList()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
/** 导出按钮操作 */
const handleExport = () => {
  // this.download('system/users/export', {
  //   ...this.queryParams
  // }, `users_${new Date().getTime()}.xlsx`)
}

</script>
  